<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<button type="button">按钮1</button>
		<button type="button">按钮2</button>
		<button type="button">按钮3</button>
		<button type="button">按钮4</button>
		<button type="button">按钮5</button>
		<script type="text/javascript">
			var btns = document.getElementsByTagName('button')
			for (var i = 0; i < btns.length; i++) {
				// 方法1
				(function (i) {
					btns[i].onclick = () => {alert(i)}
				})(i)
			}
			
			for (let i = 0; i < btns.length; i++) {
				// 方法2
					btns[i].onclick = () => {alert(i)}
			}
			
		</script>
	</body>
</html>
